package function;
/*栈以顺序结构实现，用数组来实现，在ParkAndSidewalk类中使用
 *使用栈模拟停车场
 *使用栈模拟—>保存为了让路离开停车场的车辆的场地
 */
public class Stack<T> {
	private T[] elements;
	private int top;

	public Stack(int length) {
		this.elements = (T[]) new Object[length];
		this.top = -1;//空栈的top指向-1
	}

	public Stack(T[] values) {
		this(values.length + 1);
		for (int i = 0; i < values.length; i++) {
			this.top++;
			this.elements[this.top] = values[values.length-i-1];
		}
	}
	//获得栈顶元素
	public int getTop() {
		return this.top;
	}

	public void clear() {
		this.top = -1;
	}

	public boolean isEmpty() {
		return this.top == -1;
	}

	public boolean isFull() {
		return this.top == this.elements.length - 1;
	}
	//压栈
	public void push(T x) {
		if (isFull())
			throw new ArrayIndexOutOfBoundsException();
		this.elements[++this.top] = x;
	}
	//弹栈
	public T pop() {
		if (isEmpty())
			throw new java.util.NoSuchElementException();
		return elements[this.top--];
	}

	public String toString() {
		String str = "stack=";
		for (int i = this.top; i >=0 ; i --) {
			str += this.elements[i].toString();
			if (i != 0)
				str += ",";
		}
		return str;
	}
}
